package com.zhht.htzx.ui;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.api.AccountService;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ItemMssccTitleBinding;

import java.util.List;

public class BusinessMccActivity extends BaseActivity {

    private AccountRequester mAccountRequester;

    private StateHolder mStates;

    private SimpleDataBindingAdapter<LibraryInfo, ItemMssccTitleBinding> mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<LibraryInfo, ItemMssccTitleBinding>(this, R.layout.item_msscc_title, new DiffUtil.ItemCallback<LibraryInfo>() {
            @Override
            public boolean areItemsTheSame(@NonNull LibraryInfo oldItem, @NonNull LibraryInfo newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull LibraryInfo oldItem, @NonNull LibraryInfo newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemMssccTitleBinding binding, LibraryInfo item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);

            }
        };
        return new DataBindingConfig(R.layout.activity_business_mcc, BR.vm,mStates)
                .addBindingParam(BR.adapter,mAdapter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mStates.titleBar.set("所属行业");

        getLifecycle().addObserver(mAccountRequester);

        mAccountRequester.queryMccList();
        mAccountRequester.getLibraryList().observe(this, new Observer<ResponseStatus<List<LibraryInfo>>>() {
            @Override
            public void onChanged(ResponseStatus<List<LibraryInfo>> listResponseStatus) {

                if (listResponseStatus.getResponseCode()==0){

                    mStates.testList.set(listResponseStatus.getData());

                }

            }
        });

        mAdapter.setOnItemClickListener(new BaseDataBindingAdapter.OnItemClickListener<LibraryInfo>() {
            @Override
            public void onItemClick(int viewId, LibraryInfo item, int position) {
                Intent intent= new Intent();
                intent.putExtra("data",item);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

    }



}
